package com.luo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.luo.constant.MessageConstant;
import com.luo.entity.PageResult;
import com.luo.entity.QueryPageBean;
import com.luo.entity.Result;
import com.luo.pojo.TravelItem;
import com.luo.service.TravelItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Zoro
 * @date 2021年08月24
 */
@RestController
@RequestMapping("/travelItem")
public class TravelItemController {

    @Reference
    private TravelItemService travelItemService;

    /**
     * 添加一条数据
     */
    @PreAuthorize("hasAuthority('TRAVELITEM_ADD')")
    @RequestMapping("/add.do")
    public Result addTravelItem(@RequestBody TravelItem travelItem) {
        try {
            travelItemService.addTravelItem(travelItem);
            return new Result(true, MessageConstant.ADD_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_TRAVELITEM_FAIL);
        }
    }

    /**
     * 分页查询
     */
    @PreAuthorize("hasAuthority('TRAVELITEM_QUERY')")
    @RequestMapping("/pageSelect.do")
    public PageResult pageSelect(@RequestBody QueryPageBean queryPageBean) {
        PageInfo<TravelItem> travelItemPageInfo = travelItemService.pageSelect(queryPageBean);
        //返回一个json对象
        return new PageResult(travelItemPageInfo.getTotal(), travelItemPageInfo.getList());
    }

    /**
     * 根据id查询数据
     */
    @PreAuthorize("hasAuthority('TRAVELITEM_QUERY')")
    @RequestMapping("/getTravelItem.do")
    public Result getTravelItem(Integer id) {
        try {
            TravelItem travelItemById = travelItemService.getTravelItemById(id);
            return new Result(true, MessageConstant.QUERY_TRAVELITEM_SUCCESS, travelItemById);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_TRAVELITEM_FAIL);
        }

    }

    /**
     * 修改数据
     */
    @PreAuthorize(("hasAuthority('TRAVELITEM_EDIT')"))
    @RequestMapping("/edit.do")
    public Result editTravelItem(@RequestBody TravelItem travelItem) {
        try {
            travelItemService.editTravelItem(travelItem);
            return new Result(true, MessageConstant.EDIT_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_TRAVELITEM_FAIL);
        }

    }

    /**
     * 删除数据
     */
    @PreAuthorize("hasAuthority('TRAVELITEM_DELETE')")
    @RequestMapping("/delete.do")
    public Result deleteTravelItem(Integer id) {
        try {
            travelItemService.deleteTravelItem(id);
            return new Result(true, MessageConstant.DELETE_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.DELETE_TRAVELITEM_FAIL);
        }

    }

    /**
     * 查询全部自由行数据
     */
    @PreAuthorize("hasAuthority('TRAVELITEM_QUERY')")
    @RequestMapping("/selectAll.do")
    public List<TravelItem> selectAll() {

        return travelItemService.selectAll();
    }

}
